

(function() {

    var queryCourse = function() {
        $.ajax({
            url : "/admin/list_course?condition="+$('#input').val(),
            type : 'get',
            /*dataType : "text",*/
            success : function(data) {
                console.log(data);
                //console.log(data);
                /* console.log(data);
                 updateTable(data);
                 bindAction();*/
            }
        });
    };
    bindAction();
    $("#input").bind('change', queryCourse);
    $("#btn").bind('click', queryCourse);
    $('.table_in_input').mousedown(function (event) {
        event = event ? event : window.event;
        var target = event.target ? event.target : event.srcElement;
        $(target).attr('readonly','true');
    })
    function sendReq(){
        var oldContent = $(this).val();
        if(confirm("确定修改?")){
            var msg={};

            var currentTr = $(this).closest('tr');

            var cid = currentTr.find(':checkbox').attr('cid');

            var cname = currentTr.find('.table_in_input').eq(0).val();
            var credit = currentTr.find('.table_in_input').eq(1).val();
            var chour = currentTr.find('.table_in_input').eq(2).val();
            var max_people = currentTr.find('.table_in_input').eq(3).val();
            var tid = currentTr.find('option:selected').eq(0).attr('tid');
            var major_id = currentTr.find('option:selected').eq(1).attr('major_id');

            msg.cid=cid;
            msg.credit=credit;
            msg.cname=cname;
            msg.chour=chour;
            msg.max_people=max_people;
            msg.major_id=major_id;
            msg.tid=tid;
            console.log(JSON.stringify(msg));
            $.ajax({
                url : "/sc/admin/alter_course",
                type : 'post',
                data:msg,

                dataType : "text",
                success : function(data) {
                    if(data=='success'){

                        alert('修改成功!');
                    }
                    else {
                        alert('修改失败!');
                    }
                }

            });
        }
        else{
            window.location.reload();
        }
    }
    function bindAction() {
        //修改课程

        $('.table_in_input').dblclick(function (event) {
            event = event ? event : window.event;
            var target = event.target ? event.target : event.srcElement;
            $(target).attr('readonly',false);
        });
        $('.mcb').change(sendReq);
        $('.tcb').change(sendReq);
        $('.table_in_input').change(sendReq);
    }

    function updateTable(data) {
        $('.symbol').remove();
        eval("var o=" + data + "");
        for (var i = 0; i < o.length; i++) {
            var tempNode = $("<tr class='symbol' row=''></tr>");
            $(tempNode).attr('row',i);
            for (var j = 0; j < o[i].length; j++) {
                tempNode.append("<td>" + "<input class='table_in_input' readonly='true' type='text' value='" +o[i][j]+
                    "'>" + "</td>")
            }
            tempNode.append("<td><input type='checkbox'class='check_cid'/></td>");
            $(".course_table").append(tempNode);
        }
    }

    var isManChecked = function () {
        var t=$(':radio');
        t.each(function () {
            if($(this).prop('checked')&&$(this).attr('id')=='boy'){
                return true;
            }
            return false;
        })
    }
    //删除课程
    $('#delete_btn').click(function () {

        function checkedArr() {
            var a = new Array();
            var o = $('.cid_cb:checked');
            o.each(function () {
                a.push($(this).attr('cid'));
            })
            return a;
        }
        $.ajax({
            url : "../admin/delete_course",
            data:'cid='+checkedArr(),
            type : 'get',
            dataType : "text",
            success : function(data) {

                if(data=='success'){
                    window.location.reload();
                    alert('删除成功!');
                }else {
                    alert('删除失败!');
                }
            }
        });
    })

    function editableTable() {
        var inputNode = $('<input type="text" class="table_in_input"/>');
        $('.course_table td')	.click(appendInput);
        function appendInput(event){
            event = event ? event : window.event;
            var target = event.target ? event.target : event.srcElement;
            var content = $(target).html();
            $(inputNode).attr('value',content);
            $(target).html("");
            $(target).append(inputNode);
            $(target).unbind('click',appendInput);
        }
        $('.course_table td  :input[text]').change(function (event) {
            console.log(event);
            event = event ? event : window.event;
            var target = event.target ? event.target : event.srcElement;
            var content = $(target).attr('value');
            console.log(content);
            $(target).remove();
            $(target).parent().val(content);
        });
    }
})();